package javase.ch05.homeWork.Demo3;

import java.io.IOException;
import java.net.ConnectException;
import java.net.Socket;
import java.util.concurrent.CountDownLatch;

public class TcpClient {
    public static void main(String[] args) {
        System.out.println("---------- ClientForYou ----------");
        CountDownLatch countDownLatch = new CountDownLatch(1);
        try {
            Socket socket = new Socket("127.0.0.1", 10086);

            System.out.println("============Client:" + socket.getRemoteSocketAddress() + "已上线============");

            new Thread(new WriterThread(socket, countDownLatch)).start();
            new Thread(new ReadThread(socket, countDownLatch)).start();
            try {
                countDownLatch.await(); //等待
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }catch (ConnectException e){
            System.out.println("服务器繁忙，请稍后重试");
        }
        catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
